#!/bin/bash
#本文件用于为其他脚本导入临时变量

# imports
. scripts/utils.sh

export CORE_PEER_TLS_ENABLED=true
export ORDERER_CA=${PWD}/organizations/ordererOrganizations/project.com/tlsca/tlsca.project.com-cert.pem
export ORDERER_ADMIN_TLS_SIGN_CERT=${PWD}/organizations/ordererOrganizations/project.com/orderers/orderer.project.com/tls/server.crt
export ORDERER_ADMIN_TLS_PRIVATE_KEY=${PWD}/organizations/ordererOrganizations/project.com/orderers/orderer.project.com/tls/server.key

export PEER0_ORG1_CA=${PWD}/organizations/peerOrganizations/org1.project.com/tlsca/tlsca.org1.project.com-cert.pem
export PEER0_ORG2_CA=${PWD}/organizations/peerOrganizations/org2.project.com/tlsca/tlsca.org2.project.com-cert.pem
export PEER0_ORG3_CA=${PWD}/organizations/peerOrganizations/org3.project.com/tlsca/tlsca.org3.project.com-cert.pem

#为peerX.orgX设置环境变量
setGlobals() {
  local USING_ORG=""
  if [ -z "$OVERRIDE_ORG" ]; then
    USING_ORG=$1
  else
    USING_ORG="${OVERRIDE_ORG}"
  fi
  infoln "Using organization ${USING_ORG}"
  if [ $USING_ORG -eq 1 ]; then
    export CORE_PEER_LOCALMSPID="Org1MSP"
    export CORE_PEER_TLS_ROOTCERT_FILE=$PEER0_ORG1_CA
    export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org1.project.com/users/Admin@org1.project.com/msp
    export CORE_PEER_ADDRESS=localhost:7057
  elif [ $USING_ORG -eq 2 ]; then
    export CORE_PEER_LOCALMSPID="Org2MSP"
    export CORE_PEER_TLS_ROOTCERT_FILE=$PEER0_ORG2_CA
    export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org2.project.com/users/Admin@org2.project.com/msp
    export CORE_PEER_ADDRESS=localhost:7062
  elif [ $USING_ORG -eq 3 ]; then
    export CORE_PEER_LOCALMSPID="Org3MSP"
    export CORE_PEER_TLS_ROOTCERT_FILE=$PEER0_ORG3_CA
    export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org3.project.com/users/Admin@org3.project.com/msp
    export CORE_PEER_ADDRESS=localhost:7060
  else
    errorln "ORG Unknown"
  fi

  if [ "$VERBOSE" == "true" ]; then
    env | grep CORE
  fi
}

#parsePeerConnectionParameters $@
#设置链码和peer节点之间的连接函数，在应用中很重要
#接受的参数是1,2,3
parsePeerConnectionParameters() {
  PEER_CONN_PARMS=()
  PEERS=""
  while [ "$#" -gt 0 ]; do #while(i>0)
    #根据参数(1,2,3)设置环境变量
    setGlobals $1
    if [ "$1" -eq 1 ]
    then
      PEER="peer0.org1"
    elif [ "$1" -eq 2 ]; then
      PEER="peer0.org2"
    else [ "$1" -eq 3 ]
      PEER="peer0.org3"
    fi

    if [ -z "$PEERS" ] #如果PEERS数组内没有元素
    then
	    PEERS="$PEER"
    else #PEERS数组内已有元素，则再添加新的数组成员
	    PEERS="$PEERS $PEER"
    fi

    #在PEER_CONN_PARMS的后面加上新的成员:(--peerAddresses表明这是peer地址)$CORE_PEER_ADDRESS
    #$CORE_PEER_ADDRESS已经通过setGlobals $1设置好了
    PEER_CONN_PARMS=("${PEER_CONN_PARMS[@]}" --peerAddresses $CORE_PEER_ADDRESS)
    
    #设置TLS路径
    if [ "$1" -eq 1 ]
    then
      CA=PEER0_ORG1_CA
    elif [ "$1" -eq 2 ];then
      CA=PEER0_ORG2_CA 
    else [ "$1" -eq 3 ]
      CA=PEER0_ORG3_CA
    fi
    
    #${!CA}表示所有以CA标识的值，即PEER0_ORG1_CA,PEER0_ORG2_CA,PEER0_ORG3_CA
    TLSINFO=(--tlsRootCertFiles "${!CA}")
    PEER_CONN_PARMS=("${PEER_CONN_PARMS[@]}" "${TLSINFO[@]}")
    
    shift #i--
  done
}

#检验指令运行结果
verifyResult() {
  if [ $1 -ne 0 ]; then
    fatalln "$2"
  fi
}
